//
//  AddressViewController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/12.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class FamilyAddressController: BaseViewController {
    
    private var viewModel: FamilyViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    var selectClosure: ((_ vm: FamilyRowViewModel) -> Void)?
    
    var fromProfile = false

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 0, right: 0)
            tableView.backgroundColor = .background
            tableView.rowHeight = 85
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.tableFooterView = UIView()
            tableView.showsHorizontalScrollIndicator = false
            tableView.register(FamilyAddressCell.nibForCell(),
                               forCellReuseIdentifier: FamilyAddressCell.reuseIdentifier())
        }
    }
}

extension FamilyAddressController {
    func setupUI() {
        title = "收货地址"
        
        createRightBarBtnItem(title: "添加地址", method: #selector(addAddress))
        
        tableView.mj_header = refreshHeader(action: #selector(loadData))
        tableView.mj_header?.beginRefreshing()
    }
    
    func endRefresh() {
        tableView.mj_header?.endRefreshing()
    }
    
    @objc func loadData() {
        FamilyViewModel.getReceiveAddressList(success: { models in
            self.endRefresh()
            self.viewModel = FamilyViewModel(models: models)
        }) { errorMsg in
            self.endRefresh()
            HUDTool.showError(message: errorMsg)
        }
    }
    
    @objc func addAddress() {
        let addressAddVc = FamilyAddressAddController()
        navigationController?.pushViewController(addressAddVc, animated: true)
        addressAddVc.updateClosure = { [weak self] in
            guard let self = self else { return }
            self.loadData()
        }
    }
}

extension FamilyAddressController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: FamilyAddressCell.reuseIdentifier(),
                                                 for: indexPath) as! FamilyAddressCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        cell.delegate = self
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let vm = viewModel?.viewModel(for: indexPath) else { return }
        
        if fromProfile {
            pushAddressVc(vm)
        } else {
            selectClosure?(vm)
            navigationController?.popViewController(animated: true)
        }
    }
}

extension FamilyAddressController: FamilyAddressCellDelegate {
    func addressViewCell(didClickedEdit cell: FamilyAddressCell) {
        if let indexPath = tableView.indexPath(for: cell),
            let vm = viewModel?.viewModel(for: indexPath) {
            pushAddressVc(vm)
        }
    }
    
    private func pushAddressVc(_ vm: FamilyRowViewModel) {
        let addressVc = FamilyAddressAddController(addressId: vm.receiveId)
        navigationController?.pushViewController(addressVc, animated: true)
        addressVc.updateClosure = { [weak self] in self?.loadData() }
    }
}
